﻿//by  张海军 710605420@qq.com
using System.Threading;
using UnityEngine;

namespace LazyFramework
{
    public abstract class MonoSingleton<T> : MonoBehaviour
   where T : MonoSingleton<T>
    {

        private static T m_Instance = null;
        public static T Instance
        {
            get
            {
                if (m_Instance == null)
                {
                    m_Instance = new GameObject(typeof(T).Name, typeof(T)).GetComponent<T>();
                    DontDestroyOnLoad(m_Instance);
                    m_Instance.Init();
                }
                return m_Instance;
            }
        }

        private void Awake()
        {
            if (m_Instance == null)
            {
                m_Instance = this as T;
                m_Instance.Init();
            }
            else
            {
                Destroy(this);
            }
        }

        protected virtual void Init() { }
        protected virtual void DisInit() { }
        private void OnDestroy()
        {
            if (m_Instance != null)
            {
                DisInit();
                m_Instance = null;
            }
        }

        private void OnApplicationQuit()
        {
            // mutex.ReleaseMutex();
        }
    }

}
